var roleId = getUrlParam("roleId");
layui.config({
    base: ctx + 'static/lib/layui_exts/dtree/' //配置 layui 第三方扩展组件存放的基础目录
}).extend({
    dtree: 'dtree' //定义该组件模块名
}).use(['element','layer','table','dtree'],  function () {
    var dtree = layui.dtree
    ,form = layui.form;
    layer = layui.layer;
    axios.all([axios.get(ctx + 'sys/role/info/' + roleId), axios.get(ctx + 'sys/menu/tree')]).then(axios.spread(function (infoRes, roleRes) {
        var roleInfo = infoRes.data.role;
        var roleList = roleRes.data;
        roleList.data = parseChecked(roleInfo.menuIdList, roleList.data);
        console.log(roleList);
        var DTree = dtree.render({
            elem: "#menu-tree",  //绑定元素
            data: roleList.data,
            checkbar: true,
            async: false,
            initLevel: 1
        });
        $('.wrapper').html(
            '<div class="layui-form-item">'
            +   '<label class="layui-form-label">*角色名称</label>'
            +   '<div class="layui-input-block">'
            +       '<input type="text" name="roleName" autocomplete="off" placeholder="角色名称" class="layui-input" value="' + roleInfo.roleName + '">'
            +   '</div>'
            + '</div>'
            + '<div class="layui-form-item">'
            +   '<label class="layui-form-label">*备注</label>'
            +   '<div class="layui-input-block">'
            +       '<input type="text" name="remark" autocomplete="off" placeholder="备注" class="layui-input" value="' + (roleInfo.remark ? roleInfo.remark : '') + '">'
            +   '</div>'
            + '</div>');
        form.render();
    })).catch(function (err) {
        console.log(err)
    });
    form.on('submit(add)', function (data) {
        console.log(data.field);
        var params = dtree.getCheckbarNodesParam("menu-tree");
        var ids = _.map(params, function (item) {
            return item.nodeId;
        });
        data.field['roleId'] = roleId;
        data.field['menuIdList'] = ids;
        axios.post(ctx + 'sys/role/update', data.field).then(function (res) {
            if(res.data.code == 0) {
                layer.msg('保存成功');
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.table.reload();
            }else {
                layer.msg('保存失败');
            }
        }).catch(function (err) {
           console.log(err);
        });
        return false;
    });
});

$('#cancel').click(function () {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
});

function parseChecked(checkId, roleList) {
    for(var i =0; i < roleList.length; i ++) {
        for(var j = 0; j < checkId.length; j ++) {
            if(roleList[i].id == checkId[j]) {
                roleList[i].checkArr[0].checked = 1
            }
        }
        if(roleList[i].children.length > 0) {
            parseChecked(checkId, roleList[i].children);
        }
    }
    console.log(roleList);
    return roleList;
}